package site.xuqing.androidbase.net.flash;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

/**
 * @author xuqing
 * @Description TODO
 * @createTime 2022年01月12日 10:24:34
 */
public class SwipeFreshLayoutManager implements IFlashViewManager{
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private final static Handler HANDLER=new Handler(Looper.getMainLooper());
    @Override
    public void showImmediate() {
        mSwipeRefreshLayout.setRefreshing(true);
    }
    
    @Override
    public void showDelay(long millisecond) {
        HANDLER.postDelayed(() -> {
            mSwipeRefreshLayout.setRefreshing(true);
        }, millisecond);
    }
    
    @Override
    public void cancel() {
        HANDLER.removeCallbacksAndMessages((Object)null);
        mSwipeRefreshLayout.setRefreshing(false);
    }
    
    @NonNull
    @Override
    public IFlashViewManager setFlashView(@NonNull Object flashView) {
        if (flashView instanceof SwipeRefreshLayout){
            mSwipeRefreshLayout=(SwipeRefreshLayout) flashView;
        }else{
            throw new RuntimeException("flashView mast instance of SwipeRefreshLayout");
        }
        return this;
    }
}
